package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.core.db.dao.ext.RolePermissionDao;
import com.zhuss.core.db.entity.RolePermission;

/**
 * 查询角色、权限关系 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("rolePermissionRead")
public class RolePermissionRead {
	
	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	/**
	 * 根据角色查询权限列表
	 * @param roleId
	 * @return
	 */
	public Object listPermissionId(Long roleId) {
		List<RolePermission> permissionList = rolePermissionDao.listPermission(Arrays.asList(roleId));
		List<Long> permissionIdList = new ArrayList<Long>();
		transferPermission(permissionList, permissionIdList);
		return permissionIdList;
	}
	
	
	/**
	 * 根据角色列表 查询 所拥有的权限列表ID
	 * @param roleIdList
	 * @return
	 */
	public Object listPermissionId(List<Long> roleIdList) {
		List<Long> result = rolePermissionDao.listPermissionId(roleIdList);
		return result;
	}
	
	
	private void transferPermission(List<RolePermission> permissionList,
			List<Long> permissionIdList) {
		for (RolePermission item : permissionList) {
			permissionIdList.add(item.getPermissionId());
		}
		
	}
	
	/**
	 * 根据权限查询角色列表
	 * @param roleIdList
	 * @return
	 */
	public List<Long> listRoleId(List<Long> permissionIdList) {
		List<RolePermission> roleList = new ArrayList<RolePermission>();
		for (Long permissionId : permissionIdList) {
			roleList.addAll(rolePermissionDao.listRole(permissionId));
		}
		List<Long> roleIdList = new ArrayList<Long>();
		transferRole(roleList, roleIdList);
		return roleIdList;
	}

	/**
	 * 根据权限查询角色列表
	 * @param roleId
	 * @return
	 */
	public Object listRoleId(Long permissionId) {
		List<RolePermission> roleList = rolePermissionDao.listRole(permissionId);
		List<Long> roleIdList = new ArrayList<Long>();
		transferRole(roleList, roleIdList);
		return roleIdList;
	}

	private void transferRole(List<RolePermission> roleList, List<Long> roleIdList) {
		for (RolePermission item : roleList) {
			roleIdList.add(item.getRoleId());
		}
		
	}
	
	

}
